<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>SM国密测试</title>
        
        

        <script src="js/crypto-js.js"></script>
		<script src="js/jquery-3.6.0.min.js"></script>    
		<script src="js/sm2.js"></script>
		<script src="js/sm4_2.js"></script>
      
	</head>
	<body>
		<div style="width:100rem;height:10rem;">
			<button id="btn2">SM2测试</button>
			<p id="sm2"></p>
		</div>
		<div style="width:100rem;height:10rem;">
			<button id="btn4">SM4测试</button>
			<p id="sm4"></p>
		</div>
		
			
		
		<script>
			
			$(function(){
				$("#btn2").click(function(){
					
					var pubkeySM2 = "04597cea81267d1e78598714eeb117fc5748914841719e8e11945c5b7e8dc1ceb4a8a7381ea7c6c35d8c78a9f78a65582e6e0ca5267af30e6e068818ea514c1094";
					
					const cipherMode = 0;
					var msg = "123456qazQAZ";
					
					var encryptData = sm2Encrypt(msg, pubkeySM2, cipherMode);
					//alert(encryptData);
					
					var obj={
						text:encryptData
					}
					
					$.ajax({
					  type: "POST",
					  url: "http://localhost:8080/sm2/decode",
					  data: JSON.stringify(obj),
					  dataType: "json",
					  contentType: "application/json;charset=utf-8",
					  timeout: 10000,
					  success: function(data){
						$("#sm2").html(data.out)
						//alert("success:"+data.out);
					  },
					  error: function(data){
						alert("error");
					  }
					});

				});
				
				
				$("#btn4").click(function(){
					//https://blog.csdn.net/windows_apple/article/details/122120975
					
					//SM4 CBC模式
					var keySM4 = "d8f37edaf21f4373";
					var ivSM4 = "ed32fb5d34388842";
					
					var msg = "1234561qaz@WSX中国啊啊啊啊啊啊!!@#$%^&()";
					

					var s4 = new SM4Util();
					s4.secretKey= keySM4;
					s4.iv = ivSM4;
					var encryptData = s4.encryptData_CBC(msg);
					
					
					var obj={
						text:encryptData
					}
					
					$.ajax({
					  type: "POST",
					  url: "http://localhost:8080/sm4/decode",
					  data: JSON.stringify(obj),
					  dataType: "json",
					  contentType: "application/json;charset=utf-8",
					  timeout: 10000,
					  success: function(data){
						//alert("success:"+data.out);
						var result= data.out;
						//alert(result);
						var plain = s4.decryptData_CBC(result);
						$("#sm4").html(plain);
						//alert(plain);

					  },
					  error: function(data){
						alert("error");
					  }
					});

				})
			})
			
			
		</script>
	</body>
</html>